import getFont from "../../../utils/getFont";
import { runCommand } from ".";

const getNewFont = (font, fontFamily) => {
    const fontObj = getFont(font);
    const newFont = [];
    newFont.push(fontObj.fontWeight);
    newFont.push(fontObj.fontStyle);
    newFont.push(fontObj.fontSize+'px');
    newFont.push(fontFamily);
    return newFont.join(' ');
}
export default {
    canUndo: true,
    execute: (context, options, isUndo) => {
        runCommand(context, options, isUndo, (sheet, options) => {
            const { selections, fontFamily } = options;
            selections.forEach(range => {
                for (let row = range.row, i = 0; i < range.rowCount; i++ , row++) {
                    for (let col = range.col, j = 0; j < range.colCount; j++ , col++) {
                        const cell = sheet.getCell(row, col);
                        const oldFont = cell.font();
                        const newFont = getNewFont(oldFont, fontFamily);
                        cell.font(newFont);
                    }
                }
            });
        })
    }
};
